﻿namespace StatePattern
{
    /*
    public class Account
    {
        public State State { get; set; }
        public string Owner { get; set; }

        public Account(string owner) 
        {
            this.Owner = owner;
            this.State = new SilverState(0.0, this);
        }
        public double Balance { get { return State.Balance; } } // 余额

        // 存钱
        public void Deposit(double amount) 
        {
            State.Deposit(amount);
            Console.WriteLine("存款金额为 {0:C}--", amount);
            Console.WriteLine("账户余额为 =:{0:C}", this.Balance);
            Console.WriteLine("账户状态为：{0}", this.State.GetType().Name);
            Console.WriteLine();
        }

        // 取钱
        public void Withdraw(double amount) 
        {
            State.Withdraw(amount);
            Console.WriteLine("取款金额为 {0:C}--", amount);
            Console.WriteLine("账户余额为 =:{0:C}", this.Balance);
            Console.WriteLine("账户状态为：{0}", this.State.GetType().Name);
            Console.WriteLine();
        }

        // 获得利息
        public void PayInterset()
        {
            State.PayInterest();
            Console.WriteLine("Interest Paid --- ");
            Console.WriteLine("账户余额为 =:{0:C}", this.Balance);
            Console.WriteLine("账户状态为：{0}", this.State.GetType().Name);
            Console.WriteLine();
        }
    }

    // 抽象状态类
    public abstract class State 
    {
        // Properties
        public Account Account { get; set; }
        public double Balance { get; set; } // 余额
        public double Interest { get; set; } // 利率
        public double LowerLimit { get; set; } // 下限
        public double UpperLimit { get; set; } // 上限

        public abstract void Deposit(double amount);// 存款
        public abstract void Withdraw(double amount); // 取钱
        public abstract void PayInterest(); // 获得的利息

    }

    // Red State 意味着Account透支了
    public class RedState : State 
    {
        public RedState(State state) 
        {
            // Initialize
            this.Balance = state.Balance;
            this.Account = state.Account;
            Interest = 0.00;
            LowerLimit = -100.00;
            UpperLimit = 0.00;
        }

        // 存款
        public override void Deposit(double amount)
        {
            Balance += amount;
            StateChangeCheck();
        }
        // 取钱
        public override void Withdraw(double amount)
        {
            Console.WriteLine("没有钱可以取了");
        }
        public override void PayInterest()
        {
            // 没有利息
        }
        private void StateChangeCheck() 
        {
            if (Balance > UpperLimit) 
            {
                Account.State = new SilverState(this);
            }
        }
    }

    // Silver State 意味着没有利息的
    public class SilverState : State 
    {
        public SilverState(State state) 
            :this(state.Balance, state.Account)
        {
            
        }

        public SilverState(double balance, Account account) 
        {
            this.Balance = balance;
            this.Account = account;
            Interest = 0.00;
            LowerLimit = 0.00;
            UpperLimit = 1000.00;
        }
        public override void Deposit(double amount) 
        {
            Balance += amount;
            StateChangeCheck();
        }

        public override void Withdraw(double amount)
        {
            Balance -= amount;
            StateChangeCheck();
        }
        public override void PayInterest()
        {
            Balance += Interest * Balance;
            StateChangeCheck();
        }

        private void StateChangeCheck()
        {
            if (Balance < LowerLimit)
            {
                Account.State = new RedState(this);
            }
            else if (Balance > UpperLimit)
            {
                Account.State = new GoldState(this);
            }

        }
    }

    // Gold State 意味着有利息状态
    public class GoldState : State
    {
        public GoldState(State state) 
        {
            this.Balance = state.Balance;
            this.Account = state.Account;
            Interest = 0.05;
            LowerLimit = 1000.00;
            UpperLimit = 1000000.00;
        }

        // 存钱
        public override void Deposit(double amount) 
        {
            Balance += amount;
            StateChangeCheck();
        }

        // 取钱
        public override void Withdraw(double amount) 
        {
            Balance -= amount;
            StateChangeCheck();
        }

        public override void PayInterest() 
        {
            Balance += Interest * Balance;
            StateChangeCheck();
        }

        private void StateChangeCheck() 
        {
            if (Balance < 0.0)
            {
                Account.State = new RedState(this);
            }
            else if (Balance < LowerLimit)
            {
                Account.State = new SilverState(this);
            }
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            // 开一个新的账户
            Account account = new Account("Learning Hard");

            // 进行交易
            // 存钱
            account.Deposit(1000.0);
            account.Deposit(200.0);
            account.Deposit(600.0);

            // 付利息
            account.PayInterset();

            // 取钱
            account.Withdraw(2000.0);
            account.Withdraw(500.0);

            // 等待用户输入
            Console.ReadKey();
        }
    }
    */

    /*
    // 抽象牌友类
    public abstract class AbstractCardPartnet 
    {
        public int MoneyCount { get; set; }
        public AbstractCardPartnet() 
        {
            MoneyCount = 0;
        }

        public abstract void ChangeCount(int count, AbstractMediator mediator);
    }

    // 牌友A类
    public class ParterA : AbstractCardPartnet 
    {
        // 依赖与抽象中介者对象
        public override void ChangeCount(int count, AbstractMediator mediator)
        {
            mediator.ChangeCount(count);
        }
    }

    // 牌友B类
    public class ParterB : AbstractCardPartnet
    {
        // 依赖与抽象中介者对象
        public override void ChangeCount(int count, AbstractMediator mediator)
        {
            mediator.ChangeCount(count);
        }
    }

    // 抽象状态类
    public abstract class State 
    {
        protected AbstractMediator meditor;
        public abstract void ChangeCount(int count);
    }

    // A赢状态类
    public class AWinState : State 
    {
        public AWinState(AbstractMediator concretemediator) 
        {
            this.meditor = concretemediator;
        }

        public override void ChangeCount(int count)
        {
            foreach (AbstractCardPartnet p in meditor.list)
            {
                ParterA a = p as ParterA;
                if (a != null)
                {
                    a.MoneyCount += count;
                }
                else 
                {
                    p.MoneyCount -= count;
                }
            }
        }
    }

    // B赢状态类
    public class BWinState : State
    {
        public BWinState(AbstractMediator concretemediator) 
        {
            this.meditor = concretemediator;
        }
        public override void ChangeCount(int count)
        {
            foreach (AbstractCardPartnet p in meditor.list) 
            {
                ParterB b = p as ParterB;
                // 如果集合对象中是B对象，则对B的钱添加
                if (b != null) 
                {
                    b.MoneyCount += count;
                }
                else 
                { 
                    p.MoneyCount -= count;
                }
            }
        }
    }

    // 初始化状态类
    public class InitState : State 
    {
        public InitState() 
        {
            Console.WriteLine("游戏才刚刚开始，暂时还有玩家胜出");
        }
        public override void ChangeCount(int count)
        {
            return;
        }
    }

    // 抽象中介者类
    public abstract class AbstractMediator
    {
        public List<AbstractCardPartnet> list = new List<AbstractCardPartnet>();
        public State State { get; set; }

        public AbstractMediator(State state)
        {
            this.State = state;
        }

        public void Enter(AbstractCardPartnet partnet) 
        {
            list.Add(partnet);
        }
        public void Exit(AbstractCardPartnet partnet) 
        {
            list.Remove(partnet);
        }
        public void ChangeCount(int count) 
        {
            State.ChangeCount(count);
        }
    }

    // 具体中介者类
    public class MediatorPater : AbstractMediator
    {
        public MediatorPater(State initState)
            : base(initState)
        {
            
        }
    }

    class Program 
    {
        static void Main(string[] args) 
        {
            AbstractCardPartnet A = new ParterA();
            AbstractCardPartnet B = new ParterB();
            // 初始钱
            A.MoneyCount = 20;
            B.MoneyCount = 20;

            AbstractMediator mediator = new MediatorPater(new InitState());

            // A,B玩家进入平台进行游戏
            mediator.Enter(A);
            mediator.Enter(B);

            // A赢了
            mediator.State = new AWinState(mediator);
            mediator.ChangeCount(5);
            Console.WriteLine("A 现在的钱是：{0}", A.MoneyCount); // 应该是25
            Console.WriteLine("B 现在的钱是：{0}", B.MoneyCount); // 应该是15

            // B赢了
            mediator.State = new BWinState(mediator);
            mediator.ChangeCount(10);
            Console.WriteLine("A 现在的钱是：{0}", A.MoneyCount); // 应该是25
            Console.WriteLine("B 现在的钱是：{0}", B.MoneyCount); // 应该是15
            Console.Read();
        }
    }
    */
}
